define(function (require, exports, module) {
    'use strict';
    var xdate = require('xdate');

    function ctrl($scope,$state,$timeout,$stateParams,WebService,CommonService,InfoService) {

        var par = $scope.param = new Array();

        //回退固定为持仓页
        $scope.onBackKeyDown = function(){
            $state.go("tab.account-shares");
        };

        $scope.$on('$ionicView.beforeEnter', function() {
            $scope.holdFund = {};   //持仓信息
            $scope.prodInfo = {};   //产品信息

            par.sys_time = new XDate(new Date()).toString('yyyyMMdd');
            par.allotNo = $stateParams['allotNo'];
            par.prodCode = $stateParams['prodCode'];
            par.prodSource = $stateParams['prodSource'];

            init();
        });

        $scope.doRefresh = function() {
            init();
            $scope.$broadcast('scroll.refreshComplete');
        };

        function init (){
            //查询份额信息
            WebService.getShares(par.prodSource,par.allotNo,par.prodCode).then(
                function (data) {
                    if(data.length == 1) {
                        $scope.holdFund = data[0];
                        if("2" == par.prodSource){
                            $scope.holdFund.total_income = $scope.holdFund.income_balance;
                        }
                        queryProdInfo();
                    } else {
                        CommonService.showAlert({message:"持仓信息查询失败",onUnblock:function (){
                            $timeout(function(){
                                $scope.onBackKeyDown();
                            },3000);
                        }});
                    }
                }
            );
        }

        function queryProdInfo(){
            InfoService.getFundGeneral(par.prodCode).then(function (data){
                if(data){
                    $scope.prodInfo = data;

                    productPageUrl();
                }
            });
        }

        function productPageUrl() {
            $scope.prodInfo.url = CommonService.productPageUrl($scope.prodInfo.prod_profit_mode, $scope.prodInfo.prod_source);
            if ($scope.prodInfo.url == null || typeof($scope.prodInfo.url) == "undefined"){
                CommonService.showConfig({message: "产品收益模式未配置:" + $scope.prodInfo.prod_code});
            }
        }

        $scope.gatherDetail = function (share_desc) {
            CommonService.showConfig({okText: '关闭',message : share_desc});
        }
    }

    ctrl.$inject = ['$scope','$state','$timeout','$stateParams','WebService','CommonService','InfoService'];
    return ctrl;
});